﻿using System;
using System.Data;
using System.Linq;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Security;

namespace ASPNETVN.PORTAL.Components.Share
{
    public partial class Department
    {
        public static IList<Category> SelectBy_UserID_ModuleID(int userID, Guid moduleID)
        {
            const string spName = "[dbo].[p_SHARE_Department_SelectBy_UserID_ModuleID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@UserID", SqlDbType.Int, userID);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            IDataReader reader = db.ExecuteReader(dbCommand);
            IList<Category> collection = new List<Category>();
            while (reader.Read())
            {
                Category entity = new Category();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ParentID"))) entity.ParentID = reader.GetInt32(reader.GetOrdinal("ParentID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
                if (!reader.IsDBNull(reader.GetOrdinal("SortOrder"))) entity.SortOrder = reader.GetInt32(reader.GetOrdinal("SortOrder"));
                if (!reader.IsDBNull(reader.GetOrdinal("RequiredApproved"))) entity.RequiredApproved = reader.GetBoolean(reader.GetOrdinal("RequiredApproved"));

                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }        

        public static DataSet SelectBy_ModuleID(int moduleID)
        {
            string spName = "p_SHARE_Department_SelectBy_ModuleID";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@ModuleID", DbType.Guid, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }
    }
}
